package Models
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.LocalConnection;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.events.FlexEvent;
	import mx.events.Request;

	public class Model
	{
		private var conn1to2 : LocalConnection = new LocalConnection();
		private var conn2to1 : LocalConnection = new LocalConnection();
		
		[Bindable]
		public var xml : String;
		
		[Bindable]
		public var selection : String;
		
		[Bindable]
		public var types : ArrayCollection;
		
		[Bindable]
		public var operations : ArrayCollection;
		
		[Bindable]
		public var recievedText : String;
		
		private var loader : URLLoader;
		
		public function Model()
		{
			conn2to1.allowDomain("*");
			conn2to1.client = this;
			conn2to1.connect("From2To1Connection");	
		}
		
		public function Init() : void
		{
			operations = new ArrayCollection(["<", "<=", "=", ">=", ">"]);
			var request : URLRequest = new URLRequest("data.xml");
			loader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			loader.load(request);
			loader.addEventListener(Event.COMPLETE, onLoadComplete);
		} 
		
		public function onLoadComplete (event : Event) : void
		{
			xml = loader.data;
			setTypes();
		}
		
		private function setTypes() : void
		{
			var root : XML = new XML(xml);
			types = new ArrayCollection();
			for each (var type : XML in root..@type)
			{
				if (!types.contains(type.toString()))
				{
					types.addItem(type.toString());
				}
			}
		}
		
		public function SelectSquare(operation : String, x : int) : void
		{
			if (xml != null)
			{
				var xmlSelection : XML = <selection />;
				var root : XML = new XML(xml);
				for each (var pod : XML in root..pod.(GoodSquare(operation, x, @width * @height)))
				{
					xmlSelection.appendChild(pod);
				}
				
				selection = xmlSelection.toString();
				if (selection == "")
				{
					selection = "<selection />";
				}
			}
		}
		
		public function SelectType(type : String) : void
		{
			if (xml != null)
			{
				var xmlSelection : XML = <selection />;
				var root : XML = new XML(xml);
				for each (var pod : XML in root..pod.(@type == type))
				{
					xmlSelection.appendChild(pod);
				}
				
				selection = xmlSelection.toString();
				if (selection == "")
				{
					selection = "<selection />";
				}
			}
		}			
		
		private function GoodSquare(operation : String, x: int, square : int ) : Boolean
		{
			switch (operation)
			{
				case "<":
					return square < x;
				case "<=":
					return square <= x;
				case "=":
					return square == x;
				case ">=":
					return square >= x;
				case ">":
					return square > x;
			}
			
			return false;
		}
		
		public function SendSelection() : void
		{
			conn1to2.send("From1To2Connection", "SetExternalText", selection);
		}
		
		public function SetReceivedText( s : String) : void
		{
			recievedText = s;
		}
	}
}